<?php
/**
 *
 * User: 罗艳忠
 * Email:luozong05@126.com
 * DateTime: 2017/5/3 16:28
 */

namespace App\Http\Controllers;

use App\Models\WechatQrcode;
use EasyWeChat\Foundation\Application as EasyWeChat;
use Illuminate\Http\Request;
use Log;
use App\Services\WechatEventService;
use App\Services\WechatTextService;
use Validator;
class WechatServerController extends ServerController
{

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve(EasyWeChat $wechat, WechatEventService $eventService, WechatTextService $textService)
    {
        Log::info('request arrived.');
        $wechat->server->setMessageHandler(function($message) use($eventService, $textService){
            //return new \EasyWeChat\Message\Transfer();
            //Log::debug("message",[$message->toArray()]);
            switch ($message->MsgType) {
                case 'event':
                    return $eventService->handle($message);
                    //return '收到事件消息';
                    break;
                case 'text':
                    return $textService->handle($message);
                    //return '收到文字消息';
                    break;
                case 'image':
                    //return '收到图片消息';
                    //break;
                case 'voice':
                    //return '收到语音消息';
                    //break;
                case 'video':
                    //return '收到视频消息';
                    //break;
                case 'location':
                    //return '收到坐标消息';
                   // break;
                case 'link':
                    //return '收到链接消息';
                    //break;
                // ... 其它消息
                default:
                    return new \EasyWeChat\Message\Transfer();
                    //return '收到其它消息';
                    break;
            }
            //return "欢迎关注 overtrue！";
        });

       // Log::info('return response.');

        return $wechat->server->serve();
    }

    public function registerscene(Request $request, WechatQrcode $wechatQrcode){
        $this->request = $request;
        $this->wechatQrcodeModel = $wechatQrcode;
        $qrcode_e="";
        if($this->request->method()=="POST"){
            $this->request = $request;
            $rules = [
                "city" => "required",
                "name" => "required",
                "keyword" => "required"
            ];

            $validator = Validator::make($this->request->all(),$rules);
            if($validator->fails()){
                //var_dump($validator->errors());
                return back()->withErrors($validator->errors());
            }else{
                $param['scene_name'] = $this->request->get("city")."_".$this->request->get("name");
                $param['keyword'] = $this->request->get("keyword");

                if(!empty($this->wechatQrcodeModel->where("scene_name", $param['scene_name'])->first())){
                    return back()->with("alert",["message"=>"名称已存在","style"=>"danger"]);
                }

                $param["createtime"] = time();
                $param["ticket"] = '';
                $param["expiretime"] = 0;
                $param["scan_times"] = 0;
                $param["scan_fans"] = 0;
                $qrcode = $this->wechatQrcodeModel->create($param);

                $easyQrcode = \EasyWeChat::qrcode();
                $result = $easyQrcode->forever($qrcode->id);// 或者 $qrcode->forever("foo");
                $qrcode->ticket = $result->ticket;
                $qrcode->qrcode_url = $easyQrcode->url($result->ticket);

                $qrcode_e = $qrcode->qrcode_url;

                //$qrcode_content = file_get_contents($qrcode->qrcode_url); // 得到二进制图片内容
                //$qrcode_url = storage_path("qrcode") . md5($qrcode->id) . ".jpg";
                //file_put_contents($qrcode_url, $qrcode_content);
                //$material= \EasyWeChat::material();
                //$material_result = $material->uploadImage($qrcode_url);
                //$qrcode->qrcode_media_id = $material_result->media_id;

                if($qrcode_content = file_get_contents($qrcode->qrcode_url)) { // 得到二进制图片内容
                    $qrcode_url = "/qrcode/" . md5($qrcode->id) . ".jpg";
                    //file_put_contents($qrcode_url, $qrcode_content);
                    if (\Storage::put($qrcode_url, $qrcode_content)) {
                        $material = \EasyWeChat::material();
                        $material_result = $material->uploadImage(public_path("upload/".$qrcode_url));
                        $qrcode->qrcode_media_id = $material_result->media_id;
                    }
                }
                $qrcode->update();
            }
        }
        return view("server_registerscene", ['qrcode_url'=>$qrcode_e]);
    }

}